'use strict';

// Define the `manageApp` module
var manageApp = angular.module('manageApp', []);

manageApp.config(function($httpProvider) {
	$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
	$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

	// Override $http service's default transformRequest
	$httpProvider.defaults.transformRequest = [function(data) {
		/**
		 * The workhorse; converts an object to x-www-form-urlencoded serialization.
		 * @param {Object} obj
		 * @return {String}
		 */
		var param = function(obj) {
			var query = '';
			var name, value, fullSubName, subName, subValue, innerObj, i;

			for (name in obj) {
				value = obj[name];

				if (value instanceof Array) {
					for (i = 0; i < value.length; ++i) {
						subValue = value[i];
						fullSubName = name + '[' + i + ']';
						innerObj = {};
						innerObj[fullSubName] = subValue;
						query += param(innerObj) + '&';
					}
				} else if (value instanceof Object) {
					for (subName in value) {
						subValue = value[subName];
						fullSubName = name + '[' + subName + ']';
						innerObj = {};
						innerObj[fullSubName] = subValue;
						query += param(innerObj) + '&';
					}
				} else if (value !== undefined && value !== null) {
					query += encodeURIComponent(name) + '='
							+ encodeURIComponent(value) + '&';
				}
			}

			return query.length ? query.substr(0, query.length - 1) : query;
		};

		return angular.isObject(data) && String(data) !== '[object File]'
				? param(data)
				: data;
	}];
});




// Define the `userListController` controller on the `manageApp` module
manageApp.controller('userListController', function userListController($scope, $http) {
    //加载用户
 	$scope.loadUser = function(){
	    $http.get(ctx+"/ued/userdemoangular/getAllUsers.do").then(function (response) {
 
	         $scope.users=response.data;
	    });
    }
     //是否是添加
     $scope.addFlag=false;
     //是否正序
     $scope.orderType=false;
     

	 
	//提交修改
    $scope.submitData = function(user){
      
        var addUrl=ctx+"ued/userdemoangular/submitData.do";
		if(!$("#pwdForm").valid()){
	    	 return false;
	    }
		
		var config = {params: {addFlag: $scope.addFlag}};
			    
         $http.post(addUrl, user,config)
        .success(function(data){
           if (data.status=="success"){
            	//alert($('#pwd-form'))
            	$('#pwd-form').modal('hide'); 
            	$scope.loadUser();
            }else{
            	alert(data.message);
            }
        })    
     }
     
     //删除用户
     $scope.deleteUser = function(user){
	        var addUrl=ctx+"ued/userdemoangular/deleteUser.do";
 			 //$scope.user = angular.copy(user);
		 	 
	         $http.post(addUrl, user)
	        .success(function(data){
	           //alert(2222)
	            $scope.loadUser();
	        })   
      } 
      
      //显示修改
      $scope.editUser = function(user){
      	  $scope.user = angular.copy(user);
      	  $('#pwd-form').modal('show'); 
      	  $scope.addFlag=false;
   
      }
      
      //显示添加
      $scope.addUser = function(user){
      	  $('#pwd-form').modal('show'); 
      	  $scope.addFlag=true;
      }
      
      //排序
      $scope.sortUser=function(sortField){
          if ( $scope.orderBy==sortField){
          	  $scope.orderType=!$scope.orderType;
          }
      	  $scope.orderBy=sortField ;
      	  
      }
     
      //第一次加载
      $scope.loadUser();
 
 
});

  	        